package com.lagou.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author luohq
 * 读取file  到outpustream
 */
public class StaticResourceUtil {
    public static String getAbsolutePath(String path){
        String absolute = StaticResourceUtil.class.getResource("/").getPath();
        return absolute.replaceAll("\\\\", "/")+path;
    }

    public static void outPutStaticResource(InputStream inputStream, OutputStream outputStream) throws IOException {
        int count = 0;
        while (count == 0){
            count = inputStream.available();
        }

        int resourceSize = count;
        outputStream.write(HttpProtocolUtil.getHttpHeader200(resourceSize).getBytes());
        long written = 0;
        int byteSize = 1024;
        byte [] bytes  = new byte[byteSize];
        while (written < resourceSize){
            if(written + byteSize > resourceSize){
                 byteSize = (int) (resourceSize - written);
                 bytes = new byte[byteSize];

            }
            inputStream.read(bytes);
            outputStream.write(bytes);
            written += byteSize;
            outputStream.flush();
        }


    }
}
